package JasPerl::Util::Beans;

use strict;
use warnings;

use Module::Load;
use JasPerl::Util::Boolean;
use JasPerl::Util::Date;

my %TYPES = (
    'java.util.Date' => sub { JasPerl::Util::Date->new }
);

sub instantiate {
    my ($class, $type) = @_;

    if (my $method = $TYPES{$type}) {
        return $method->();
    } else {
        load $type;
        return $type->new;
    }
}

sub get_property {
    my ($class, $object, $property) = @_;
    # TODO: check blessed, defined property

    my $name = $property;
    # convert camel case to lower case w/underscores
    $name =~ s/([[:lower:]])([[:upper:]]+)/$1_\L$2\E/g;
    
    if (my $method = $object->can("get_$name")) {
        return scalar $object->$method;
    }
    
    if (my $method = $object->can("is_$name")) {
        return JasPerl::Util::Boolean->from($object->$method);
    }

    die "No getter for '$property' found on type $class";
}

sub set_property {
    my ($class, $object, $property, $value) = @_;
    # TODO: check blessed, defined property

    my $name = $property;
    # convert camel case to lower case w/underscores
    $name =~ s/([[:lower:]])([[:upper:]]+)/$1_\L$2\E/g;

    if (my $method = $object->can("set_$name")) {
        $object->$method($value);
    }
    
    die "No setter for '$property' found on type $class";
}

1;
